{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd04c36a90a135e4aa4256adc3692917ccc380701b533777f3da43dc8bc5264a29c",
   "display_name": "Python 3.8.5 64-bit (conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 第二次作业第8题\n",
    "作者：赵飞\n",
    "学号：202018019427078\n",
    "日期：2021-04-26"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy import sparse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "===使用coo_matrix表示===\n  (0, 0)\t3\n  (0, 2)\t8\n  (1, 1)\t2\n  (3, 3)\t1\n===使用dok_matrix表示===\n  (0, 0)\t3\n  (0, 2)\t8\n  (1, 1)\t2\n  (3, 3)\t1\n===使用lil_matrix表示===\n  (0, 0)\t3\n  (0, 2)\t8\n  (1, 1)\t2\n  (3, 3)\t1\n"
     ]
    }
   ],
   "source": [
    "A = np.array([[3, 0, 8, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]])\n",
    "A_coo = sparse.coo_matrix(A)\n",
    "A_dok = sparse.dok_matrix(A)\n",
    "A_lil = sparse.lil_matrix(A)\n",
    "print('===使用coo_matrix表示===')\n",
    "print(A_coo)\n",
    "print('===使用dok_matrix表示===')\n",
    "print(A_dok)\n",
    "print('===使用lil_matrix表示===')\n",
    "print(A_lil)\n",
    "# print(A_lil.toarray())\n",
    "# print(A_lil)\n",
    "# print(A_lil.rows)\n",
    "# print(A_lil.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "\u001b[1;31mInit signature:\u001b[0m \u001b[0msparse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcoo_matrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mDocstring:\u001b[0m     \n",
      "A sparse matrix in COOrdinate format.\n",
      "\n",
      "Also known as the 'ijv' or 'triplet' format.\n",
      "\n",
      "This can be instantiated in several ways:\n",
      "    coo_matrix(D)\n",
      "        with a dense matrix D\n",
      "\n",
      "    coo_matrix(S)\n",
      "        with another sparse matrix S (equivalent to S.tocoo())\n",
      "\n",
      "    coo_matrix((M, N), [dtype])\n",
      "        to construct an empty matrix with shape (M, N)\n",
      "        dtype is optional, defaulting to dtype='d'.\n",
      "\n",
      "    coo_matrix((data, (i, j)), [shape=(M, N)])\n",
      "        to construct from three arrays:\n",
      "            1. data[:]   the entries of the matrix, in any order\n",
      "            2. i[:]      the row indices of the matrix entries\n",
      "            3. j[:]      the column indices of the matrix entries\n",
      "\n",
      "        Where ``A[i[k], j[k]] = data[k]``.  When shape is not\n",
      "        specified, it is inferred from the index arrays\n",
      "\n",
      "Attributes\n",
      "----------\n",
      "dtype : dtype\n",
      "    Data type of the matrix\n",
      "shape : 2-tuple\n",
      "    Shape of the matrix\n",
      "ndim : int\n",
      "    Number of dimensions (this is always 2)\n",
      "nnz\n",
      "    Number of stored values, including explicit zeros\n",
      "data\n",
      "    COO format data array of the matrix\n",
      "row\n",
      "    COO format row index array of the matrix\n",
      "col\n",
      "    COO format column index array of the matrix\n",
      "\n",
      "Notes\n",
      "-----\n",
      "\n",
      "Sparse matrices can be used in arithmetic operations: they support\n",
      "addition, subtraction, multiplication, division, and matrix power.\n",
      "\n",
      "Advantages of the COO format\n",
      "    - facilitates fast conversion among sparse formats\n",
      "    - permits duplicate entries (see example)\n",
      "    - very fast conversion to and from CSR/CSC formats\n",
      "\n",
      "Disadvantages of the COO format\n",
      "    - does not directly support:\n",
      "        + arithmetic operations\n",
      "        + slicing\n",
      "\n",
      "Intended Usage\n",
      "    - COO is a fast format for constructing sparse matrices\n",
      "    - Once a matrix has been constructed, convert to CSR or\n",
      "      CSC format for fast arithmetic and matrix vector operations\n",
      "    - By default when converting to CSR or CSC format, duplicate (i,j)\n",
      "      entries will be summed together.  This facilitates efficient\n",
      "      construction of finite element matrices and the like. (see example)\n",
      "\n",
      "Examples\n",
      "--------\n",
      "\n",
      ">>> # Constructing an empty matrix\n",
      ">>> from scipy.sparse import coo_matrix\n",
      ">>> coo_matrix((3, 4), dtype=np.int8).toarray()\n",
      "array([[0, 0, 0, 0],\n",
      "       [0, 0, 0, 0],\n",
      "       [0, 0, 0, 0]], dtype=int8)\n",
      "\n",
      ">>> # Constructing a matrix using ijv format\n",
      ">>> row  = np.array([0, 3, 1, 0])\n",
      ">>> col  = np.array([0, 3, 1, 2])\n",
      ">>> data = np.array([4, 5, 7, 9])\n",
      ">>> coo_matrix((data, (row, col)), shape=(4, 4)).toarray()\n",
      "array([[4, 0, 9, 0],\n",
      "       [0, 7, 0, 0],\n",
      "       [0, 0, 0, 0],\n",
      "       [0, 0, 0, 5]])\n",
      "\n",
      ">>> # Constructing a matrix with duplicate indices\n",
      ">>> row  = np.array([0, 0, 1, 3, 1, 0, 0])\n",
      ">>> col  = np.array([0, 2, 1, 3, 1, 0, 0])\n",
      ">>> data = np.array([1, 1, 1, 1, 1, 1, 1])\n",
      ">>> coo = coo_matrix((data, (row, col)), shape=(4, 4))\n",
      ">>> # Duplicate indices are maintained until implicitly or explicitly summed\n",
      ">>> np.max(coo.data)\n",
      "1\n",
      ">>> coo.toarray()\n",
      "array([[3, 0, 1, 0],\n",
      "       [0, 2, 0, 0],\n",
      "       [0, 0, 0, 0],\n",
      "       [0, 0, 0, 1]])\n",
      "\u001b[1;31mFile:\u001b[0m           d:\\anaconda\\run\\lib\\site-packages\\scipy\\sparse\\coo.py\n",
      "\u001b[1;31mType:\u001b[0m           type\n",
      "\u001b[1;31mSubclasses:\u001b[0m     \n"
     ],
     "name": "stdout"
    }
   ],
   "source": [
    "sparse.coo_matrix?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "\u001b[1;31mInit signature:\u001b[0m \u001b[0msparse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdok_matrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mDocstring:\u001b[0m     \n",
      "Dictionary Of Keys based sparse matrix.\n",
      "\n",
      "This is an efficient structure for constructing sparse\n",
      "matrices incrementally.\n",
      "\n",
      "This can be instantiated in several ways:\n",
      "    dok_matrix(D)\n",
      "        with a dense matrix, D\n",
      "\n",
      "    dok_matrix(S)\n",
      "        with a sparse matrix, S\n",
      "\n",
      "    dok_matrix((M,N), [dtype])\n",
      "        create the matrix with initial shape (M,N)\n",
      "        dtype is optional, defaulting to dtype='d'\n",
      "\n",
      "Attributes\n",
      "----------\n",
      "dtype : dtype\n",
      "    Data type of the matrix\n",
      "shape : 2-tuple\n",
      "    Shape of the matrix\n",
      "ndim : int\n",
      "    Number of dimensions (this is always 2)\n",
      "nnz\n",
      "    Number of nonzero elements\n",
      "\n",
      "Notes\n",
      "-----\n",
      "\n",
      "Sparse matrices can be used in arithmetic operations: they support\n",
      "addition, subtraction, multiplication, division, and matrix power.\n",
      "\n",
      "Allows for efficient O(1) access of individual elements.\n",
      "Duplicates are not allowed.\n",
      "Can be efficiently converted to a coo_matrix once constructed.\n",
      "\n",
      "Examples\n",
      "--------\n",
      ">>> import numpy as np\n",
      ">>> from scipy.sparse import dok_matrix\n",
      ">>> S = dok_matrix((5, 5), dtype=np.float32)\n",
      ">>> for i in range(5):\n",
      "...     for j in range(5):\n",
      "...         S[i, j] = i + j    # Update element\n",
      "\u001b[1;31mFile:\u001b[0m           d:\\anaconda\\run\\lib\\site-packages\\scipy\\sparse\\dok.py\n",
      "\u001b[1;31mType:\u001b[0m           type\n",
      "\u001b[1;31mSubclasses:\u001b[0m     \n"
     ],
     "name": "stdout"
    }
   ],
   "source": [
    "sparse.dok_matrix?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "\u001b[1;31mInit signature:\u001b[0m \u001b[0msparse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlil_matrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mDocstring:\u001b[0m     \n",
      "Row-based list of lists sparse matrix\n",
      "\n",
      "This is a structure for constructing sparse matrices incrementally.\n",
      "Note that inserting a single item can take linear time in the worst case;\n",
      "to construct a matrix efficiently, make sure the items are pre-sorted by\n",
      "index, per row.\n",
      "\n",
      "This can be instantiated in several ways:\n",
      "    lil_matrix(D)\n",
      "        with a dense matrix or rank-2 ndarray D\n",
      "\n",
      "    lil_matrix(S)\n",
      "        with another sparse matrix S (equivalent to S.tolil())\n",
      "\n",
      "    lil_matrix((M, N), [dtype])\n",
      "        to construct an empty matrix with shape (M, N)\n",
      "        dtype is optional, defaulting to dtype='d'.\n",
      "\n",
      "Attributes\n",
      "----------\n",
      "dtype : dtype\n",
      "    Data type of the matrix\n",
      "shape : 2-tuple\n",
      "    Shape of the matrix\n",
      "ndim : int\n",
      "    Number of dimensions (this is always 2)\n",
      "nnz\n",
      "    Number of stored values, including explicit zeros\n",
      "data\n",
      "    LIL format data array of the matrix\n",
      "rows\n",
      "    LIL format row index array of the matrix\n",
      "\n",
      "Notes\n",
      "-----\n",
      "\n",
      "Sparse matrices can be used in arithmetic operations: they support\n",
      "addition, subtraction, multiplication, division, and matrix power.\n",
      "\n",
      "Advantages of the LIL format\n",
      "    - supports flexible slicing\n",
      "    - changes to the matrix sparsity structure are efficient\n",
      "\n",
      "Disadvantages of the LIL format\n",
      "    - arithmetic operations LIL + LIL are slow (consider CSR or CSC)\n",
      "    - slow column slicing (consider CSC)\n",
      "    - slow matrix vector products (consider CSR or CSC)\n",
      "\n",
      "Intended Usage\n",
      "    - LIL is a convenient format for constructing sparse matrices\n",
      "    - once a matrix has been constructed, convert to CSR or\n",
      "      CSC format for fast arithmetic and matrix vector operations\n",
      "    - consider using the COO format when constructing large matrices\n",
      "\n",
      "Data Structure\n",
      "    - An array (``self.rows``) of rows, each of which is a sorted\n",
      "      list of column indices of non-zero elements.\n",
      "    - The corresponding nonzero values are stored in similar\n",
      "      fashion in ``self.data``.\n",
      "\u001b[1;31mFile:\u001b[0m           d:\\anaconda\\run\\lib\\site-packages\\scipy\\sparse\\lil.py\n",
      "\u001b[1;31mType:\u001b[0m           type\n",
      "\u001b[1;31mSubclasses:\u001b[0m     \n"
     ],
     "name": "stdout"
    }
   ],
   "source": [
    "sparse.lil_matrix?"
   ]
  }
 ]
}